import 'package:flutter/material.dart';
import 'package:untitled1/app/res/myColors.dart';
import 'package:untitled1/app/utils/utilRouter.dart';
import 'package:untitled1/app/utils/utilScreen.dart';
import 'package:untitled1/app/widgets/MyContainer.dart';
import 'package:untitled1/app/widgets/toast/MyToast.dart';

import '../../../widgets/base_page/BaseStatefulPage.dart';
import '../employer/employer_page/employer_page_dialog/EmployerPageDialog.dart';
import 'MessageListPage.dart';
import 'home_page/SyMyBanner.dart';
import 'home_page/SyMyGridView.dart';
import 'home_page/SyMyListView.dart';
import 'home_page/SyMyListView2.dart';
//首页
class HomePage extends BaseStatefulPage {
  const HomePage({super.key}) : super();

  @override
  BaseStatefulPageState<BaseStatefulPage> createState() => _HomePageState();
}

class _HomePageState extends BaseStatefulPageState<HomePage> {
  String? _error;
  bool _isEmpty = false;

  @override
  void initState() {
    super.initState();
    // 页面初始化，加载数据
    loadData();
  }

  void loadData() async {
    safeSetState(() {
      _error = null;
      _isEmpty = false;
    });

    await Future.delayed(const Duration(seconds: 2));

    // 模拟异常或者空数据
    safeSetState(() {
      _error = null; // "网络异常";
      _isEmpty = false;
    });
  }

  @override
  void onDispose() {
    super.onDispose();
    // 释放资源
  }

  @override
  void didChangeAppLifecycleStateOnResume() {
    super.didChangeAppLifecycleStateOnResume();
    debugPrint("页面恢复可见");
  }

  @override
  void didChangeAppLifecycleStateOnPause() {
    super.didChangeAppLifecycleStateOnPause();
    debugPrint("页面进入后台");
  }

  @override
  Widget buildPage(BuildContext context) {
    if (_error != null) {
      return buildErrorView(_error);
    }
    if (_isEmpty) {
      return buildEmptyView();
    }
    return Scaffold(
      body: Container(
        color: myColors.white,
        height:utilScreen.getScreenHeight(context),
        width: utilScreen.getScreenWidth(context),
        child: Column(
          mainAxisSize: MainAxisSize.max,
          children: [
            Container(
              margin:EdgeInsets.only(top: 50),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  GestureDetector(
                      onTap: () {
                        EmployerPageDialog.dialog_citySelect(context,(data){});
                      },
                      child: Row(
                        children: [
                          const  SizedBox(width: 15,),
                          Image.asset("assets/app/home_dw.png",width: 10,height: 10,),
                          const SizedBox(width: 3,),
                          const  Text('北京市',style: TextStyle(fontSize: 14,fontWeight: FontWeight.bold,color:myColors.c_191919)),
                          const SizedBox(width: 3,),
                          const Icon(Icons.arrow_forward_ios, size: 10),
                        ],
                      )
                  ),
                  IconButton(
                      icon: Icon(Icons.notifications,size: 20,),
                      onPressed: () {
                        utilRouter.to(MessageListPage());
                      }
                  )
                ],
              ),

            ),
            Expanded(
                child: NotificationListener<ScrollNotification>(
                  onNotification: (notification) {
                    if (notification is ScrollStartNotification) {
                      if (notification.metrics.axis == Axis.horizontal) {
                        return true; // 阻止父滚动
                      }
                    }
                    return false;
                  },
                  child: SingleChildScrollView(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        SyMyBanner(),
                        SyMyGridView(),
                        SizedBox(height: 8),
                        Container(
                          margin: EdgeInsets.only(left: 10,right: 10),
                          padding: EdgeInsets.only(left: 8,right: 8,bottom: 8),
                          decoration: BoxDecoration(
                            color: myColors.c_FFF3DD,
                            borderRadius: BorderRadius.circular(8),
                          ),
                          child: Column(
                            children: [
                              getViewTitle(1, "雇主需求",myColors.c_C87C51, onTap: () {
                                MyToast.show(context, "点击了");
                              }),

                              SyMyListView()

                              // getListItemView( context,  2, 1)
                            ],
                          ),
                        ),

                        SizedBox(height: 8),
                        Container(
                          margin: EdgeInsets.only(left: 10,right: 10),
                          padding: EdgeInsets.only(left: 8,right: 8,bottom: 8),
                          decoration: BoxDecoration(
                            color: myColors.c_FDE1E4,
                            borderRadius: BorderRadius.circular(8),
                          ),
                          child: Column(
                            children: [
                              getViewTitle(1, "服务人员",myColors.c_FF7777 , onTap: () {
                                MyToast.show(context, "点击了");
                              }),

                              SyMyListView2(),

                              // getListItemView( context,  2, 1)
                            ],
                          ),
                        ),

                      ],
                    ),
                  ),
                ),


            ),
          ],
        ),
      ),
    );
  }

  Widget getViewTitle(int index, String title, Color color,{VoidCallback? onTap}) {
    return MyContainer(
      onTap: onTap ?? () {},
      height: 30,
      width: utilScreen.getScreenWidth(context),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Text(title,style: TextStyle(fontSize: 14,color: color,fontWeight: FontWeight.bold),),
          Icon(Icons.keyboard_arrow_right)
        ],

      ),
    );
  }


}






